package sanrenxing.dayEnd.backRepertoryTotal.service.impl;


import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.userDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Role;
import sanrenxing.dayEnd.backRepertoryTotal.domain.users;
import sanrenxing.dayEnd.backRepertoryTotal.service.userbiz;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service("ub")
public class userimpl implements userbiz {
    @Resource
    private userDao ud;
    @Override
    public int add(users us) {
        return ud.add(us);
    }

    @Override
    public int del(int us) {
        return ud.del(us);
    }

    @Override
    public int update(users us) {
        return ud.update(us);
    }
    @Override
    public int updates(String us) {
        String [] ss=us.split(",");
        int k=0;
        for (int i=0;i<ss.length;i++) {
            k=ud.del(Integer.parseInt(ss[i]));
        }
        return k;
    }

    @Override
    public List<users> all(String xx, int limit, int page) {
       int pages=(page-1)*limit;
       int h=page*limit;
        return ud.all(xx,h,pages);
    }

    @Override
    public int count(String xx) {
        return ud.count(xx);
    }

    @Override
    public users one(users us) {
        return ud.one(us);
    }

    @Override
    public String xx(users us) {
        String xx="";
        if(us!=null){
            if(us.getUserId()!=null){
                xx+=" and userId="+us.getUserId();
            }
            if(us.getUserName()!=null){
                xx+=" and userName like '%"+us.getUserName()+"%'";
            }if(us.getUserState()!=null){
                xx+=" and userState="+us.getUserState();
            }
        }
        return xx;
    }

    @Override
    public int dels(int us) {
        return ud.del(us);
    }

    @Override
    public List<Role> rfal() {
        return ud.rfal();
    }

    @Override
    public int updatePassword(String newPassword, String oldPassword, HttpServletRequest request) {
        HttpSession session = request.getSession();
        users user= (users) session.getAttribute("sn");
        String userId =String.valueOf(user.getUserId());
        //调用判断是否有改账号方法
        if (ud.queryPassword(userId, oldPassword)>0){
            if (ud.updatePassword(userId, newPassword)>0){
                return 1;
            }else return 2;

        }else return 3;
    }

    @Override
    public void resetPwd(String userId) {
        ud.resetPwd(userId);
    }
}
